package cn.iocoder.yudao.module.vr.controller.admin.testoption;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.vr.controller.admin.testoption.vo.*;
import cn.iocoder.yudao.module.vr.convert.testoption.TestOptionConvert;
import cn.iocoder.yudao.module.vr.dal.dataobject.testoption.TestOptionDO;
import cn.iocoder.yudao.module.vr.service.testoption.TestOptionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 试题选项")
@RestController
@RequestMapping("/vr/test-option")
@Validated
public class TestOptionController {

    @Resource
    private TestOptionService testOptionService;

    @PostMapping("/create")
    @Operation(summary = "创建试题选项")
    @PreAuthorize("@ss.hasPermission('vr:test-option:create')")
    public CommonResult<Long> createTestOption(@Valid @RequestBody TestOptionCreateReqVO createReqVO) {
        return success(testOptionService.createTestOption(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试题选项")
    @PreAuthorize("@ss.hasPermission('vr:test-option:update')")
    public CommonResult<Boolean> updateTestOption(@Valid @RequestBody TestOptionUpdateReqVO updateReqVO) {
        testOptionService.updateTestOption(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试题选项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vr:test-option:delete')")
    public CommonResult<Boolean> deleteTestOption(@RequestParam("id") Long id) {
        testOptionService.deleteTestOption(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试题选项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vr:test-option:query')")
    public CommonResult<TestOptionRespVO> getTestOption(@RequestParam("id") Long id) {
        TestOptionDO testOption = testOptionService.getTestOption(id);
        return success(TestOptionConvert.INSTANCE.convert(testOption));
    }

    @GetMapping("/list")
    @Operation(summary = "获得试题选项列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('vr:test-option:query')")
    public CommonResult<List<TestOptionRespVO>> getTestOptionList(@RequestParam("ids") Collection<Long> ids) {
        List<TestOptionDO> list = testOptionService.getTestOptionList(ids);
        return success(TestOptionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试题选项分页")
    @PreAuthorize("@ss.hasPermission('vr:test-option:query')")
    public CommonResult<PageResult<TestOptionRespVO>> getTestOptionPage(@Valid TestOptionPageReqVO pageVO) {
        PageResult<TestOptionDO> pageResult = testOptionService.getTestOptionPage(pageVO);
        return success(TestOptionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试题选项 Excel")
    @PreAuthorize("@ss.hasPermission('vr:test-option:export')")
    @OperateLog(type = EXPORT)
    public void exportTestOptionExcel(@Valid TestOptionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TestOptionDO> list = testOptionService.getTestOptionList(exportReqVO);
        // 导出 Excel
        List<TestOptionExcelVO> datas = TestOptionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "试题选项.xls", "数据", TestOptionExcelVO.class, datas);
    }

}
